package com.codeoss.framework.view.parser;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.data.metadata.Column;
import com.codeoss.framework.data.metadata.Table;
import com.codeoss.framework.data.store.ITableStore;
import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.expression.ExpressionUtil;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.Image;
import com.codeoss.framework.view.datasource.DataSourceConfig;
import com.codeoss.framework.view.util.DataSourceUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

@Service
public class ImageParser extends AbstractComponentParser {
	@Autowired
	private DataSourceUtil dataSourceUtil;
	@Autowired
	private ITableStore tableStore;
	protected void setAttributes(IParseContext parseContext, ObjectNode objectNode, Component component) {
		super.setAttributes(parseContext, objectNode, component);
		Image image = (Image) component;
		JsonNode jsonNode=objectNode.get("labelExpression");
		if(jsonNode!=null) {
			String labelExpression =jsonNode.asText();
			Expression expression = ExpressionUtil.convertLabelToExpression(labelExpression,Expression.EXPRESSION_TYPE_JAVASCRIPT);
			image.setLabelExpression(expression);
		}
		
		jsonNode=objectNode.get("col");
		if(jsonNode!=null) {
			int col=objectNode.get("col").asInt();
			image.setCol(col);
		}
	
		jsonNode=objectNode.get("fieldcol");
		if(jsonNode!=null) {
			int fieldCol=objectNode.get("fieldcol").asInt();
			image.setFieldCol(fieldCol);
		}
		ObjectNode propertyMapNode=(ObjectNode)objectNode.get("propertyMap");
		if(propertyMapNode!=null) {
			jsonNode=propertyMapNode.get("dataSource");
			if(jsonNode!=null) {
				String dataSource=propertyMapNode.get("dataSource").asText();
				image.setDataSource(dataSource);
			}
			jsonNode=propertyMapNode.get("dataField");
			if(jsonNode!=null) {
				String dataField=propertyMapNode.get("dataField").asText();
				image.setDataField(dataField);
				
				DataSourceConfig dataSourceConfig=dataSourceUtil.getDataSourceConfigById(parseContext.getPage(), image.getDataSource());
				Table table= tableStore.get(dataSourceConfig.getTableName());
				Column column=table.getColumnByColumnName(dataField);
				String requiredExpression=column.getRequiredExpression();
				if(!StringUtils.isEmpty(requiredExpression)) {
					Expression expression=ExpressionUtil.convertToExpression(requiredExpression, Expression.EXPRESSION_TYPE_JAVASCRIPT);
					image.setRequiredExpression(expression);
				}
				String readOnlyExpression=column.getReadOnlyExpression();
				if(!StringUtils.isEmpty(readOnlyExpression)) {
					Expression expression=ExpressionUtil.convertToExpression(readOnlyExpression, Expression.EXPRESSION_TYPE_JAVASCRIPT);
					image.setReadOnlyExpression(expression);
				}
				String defaultValueExpression=column.getDefaultValueExpression();
				if(!StringUtils.isEmpty(defaultValueExpression)) {
					Expression expression=ExpressionUtil.convertToExpression(defaultValueExpression, Expression.EXPRESSION_TYPE_JAVASCRIPT);
					image.setDefaultValueExpression(expression);
				}
				
				image.setDataType(column.getDataType());
				
				jsonNode=propertyMapNode.get("varName");
				String varName="";
				if(jsonNode==null ||StringUtils.isEmpty(jsonNode.asText())) {
					if(!StringUtils.isEmpty(image.getDataSource()) && !StringUtils.isEmpty(image.getDataField())) {
						varName=table.getTableName()+"_"+image.getDataField();
					}
				}else {
					varName=propertyMapNode.get("varName").asText();
				}
				image.setVarName(varName);
			}
			
		}
		jsonNode= objectNode.get("noLabel");
		if(jsonNode!=null) {
			image.setNoLabel(jsonNode.asBoolean());
		}
	}
	
	public void afterPageParse(IParseContext parseContext,Component component) {
		Image image = (Image) component;
		 DataSourceConfig dataSourceConfig=dataSourceUtil.getDataSourceConfigById(parseContext.getPage(), image.getDataSource());
		 if(dataSourceConfig==null) {
			 return;
		 }
		 Table table= tableStore.get(dataSourceConfig.getTableName());
		 Column column=table.getColumnByColumnName(image.getDataField());
		 
		 if(column!=null) {
			 Expression requiredExpression=image.getRequiredExpression();
			 if(requiredExpression!=null) {
				 parseExpression(parseContext, component, requiredExpression, "required", false);
			 }
			 
			 Expression readonlyExpression=image.getReadOnlyExpression();
			 if(readonlyExpression!=null) {
				 parseExpression(parseContext, component, readonlyExpression, "readonly", true);
			 }
			 
			 Expression defaultValueExpression=image.getDefaultValueExpression();
			 if(defaultValueExpression!=null) {
				 parseExpression(parseContext, component, defaultValueExpression, "defaultValue", false);
			 }
			 
			 Expression visibleExpression=image.getVisibleExpression();
			 if(visibleExpression!=null) {
				 parseExpression(parseContext, component, visibleExpression, "visible", true);
			 }
		 }
		 super.afterPageParse(parseContext, component);
	}
	
	@Override
	protected Component createComponent(IParseContext parseContext, ObjectNode objectNode) {
		// TODO Auto-generated method stub
		return new Image();
	}


}
